package net.xo68.destiny.manager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import net.xo68.destiny.contract.common.SystemConstants;
import net.xo68.destiny.contract.domain.sched.JobSchedResult;
import net.xo68.destiny.contract.enums.job.JobInstanceExtendEnum;
import net.xo68.destiny.contract.enums.job.JobInstanceStateEnum;
import net.xo68.destiny.core.json.JsonMapper;
import net.xo68.destiny.core.util.IpUtil;
import net.xo68.destiny.core.util.TimeUtils;
import net.xo68.destiny.manager.entity.JobInstance;
import net.xo68.destiny.manager.entity.JobInstanceExtend;
import net.xo68.destiny.manager.mapper.JobInstanceExtendMapper;
import net.xo68.destiny.manager.mapper.JobInstanceMapper;
import net.xo68.destiny.starter.common.SchedulerCommonProperties;
import net.xo68.destiny.starter.manager.sched.JobScheduleProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;



/** 
 * JobInstance Service
 * @author wuxie(16349023@qq.com)
 * @date 2020年12月21日 21:43:52
 *
 */
@Slf4j
@Service
public class JobInstanceService implements JobScheduleProvider {

    @Autowired
    private Environment environment;
    @Autowired
    private SchedulerCommonProperties schedulerCommonProperties;
    @Autowired
    private JsonMapper jsonMapper;
    @Autowired
    private JobInstanceMapper jobInstanceMapper;
    @Autowired
    private JobInstanceExtendMapper jobInstanceExtendMapper;

    public JobInstance find(Long instanceId) {
        return  jobInstanceMapper.find(instanceId);
    }

    public JobInstance insert(JobInstance jobInstance) {
        jobInstanceMapper.insert(jobInstance);
        return jobInstance;
    }

    public JobInstance update(JobInstance jobInstance) {
        jobInstanceMapper.update(jobInstance);
        return jobInstance;
    }
    
    public int delete(Long instanceId) {
        return jobInstanceMapper.delete(instanceId);
    }
    
    public List<JobInstance> list() {
        return jobInstanceMapper.list();
    }
	
	public PageInfo<JobInstance> list(int pageNumber,int pageSize) {
        PageHelper.startPage(pageNumber, pageSize,true);
        List<JobInstance> docs = jobInstanceMapper.list();
        PageInfo<JobInstance> pageInfo = new PageInfo<JobInstance>(docs);
        return pageInfo;
    }

    /**
     * 任务执行开始
     * @param instanceId
     * @param startTime
     */
    public void handleStart(Long instanceId, Date startTime){
        jobInstanceMapper.handleStart(instanceId,startTime);
    }

    /**
     * 任务执行完成
     * @param instanceId
     * @param instanceState
     * @param endTime
     */
    public void handleFinish(Long instanceId,Byte instanceState, Date endTime){
        jobInstanceMapper.handleFinish(instanceId,instanceState,endTime);
    }

    @Override
    public void schedule(JobSchedResult jobSchedResult) {
        //log.debug("jobSchedResult: {}", jobSchedResult);
        String schedHost=IpUtil.getIpPort(schedulerCommonProperties.getNodeRealIp(),environment.getProperty(SystemConstants.CONFIG_SERVER_PORT));
        if(jobSchedResult.isFail()){
            jobInstanceMapper.sched(jobSchedResult.getJobInstanceId(),schedHost, TimeUtils.nowDate(),jobSchedResult.getWorker(), JobInstanceStateEnum.SCHED_FAIL.getCode());
        }else {
            jobInstanceMapper.sched(jobSchedResult.getJobInstanceId(),schedHost, TimeUtils.nowDate(),jobSchedResult.getWorker(), JobInstanceStateEnum.SCHED_SUCCESS.getCode());
        }
        //更新实例扩展信息 JobInstanceExtendEnum
        jobInstanceExtendMapper.delete(jobSchedResult.getJobInstanceId(),JobInstanceExtendEnum.SCHED_RESULT.name());
        JobInstanceExtend instanceExtend=new JobInstanceExtend();
        instanceExtend.setInstanceId(jobSchedResult.getJobInstanceId());
        instanceExtend.setExtKey(JobInstanceExtendEnum.SCHED_RESULT.name());
        instanceExtend.setExtValue(jsonMapper.toString(jobSchedResult,false));
        jobInstanceExtendMapper.insert(instanceExtend);
    }
}